//
//  LMTabBarItemModel.m
//  LMTabBarControllerKit
//
//  Created by mengmenglu on 6/24/16.
//  Copyright © 2016 Hangzhou TaiXuan Network Technology Co., Ltd. All rights reserved.
//  
#import "LMTabBarItemModel.h"

@interface LMTabBarItemModel ()

@end

@implementation LMTabBarItemModel

#pragma mark - Public Method
- (instancetype)initWithTitle:(NSString *)title
              normalTextColor:(UIColor *)normalTextColor
              selectTextColor:(UIColor *)selectTextColor
                  normalImage:(UIImage *)normalImage
                selectedImage:(UIImage *)selectedImage {
    self = [super init];
    if (self) {
        [self initDefaultParamWith:title normalTextColor:normalTextColor selectTextColor:selectTextColor normalImage:normalImage selectedImage:selectedImage];
    }
    return self;
}


- (instancetype)initWithTitle:(NSString *)title
              normalTextColor:(UIColor *)normalTextColor
              selectTextColor:(UIColor *)selectTextColor
                  normalImage:(UIImage *)normalImage
                selectedImage:(UIImage *)selectedImage
              normalImageSize:(CGSize)normalImageSize
            selectedImageSize:(CGSize)selectedImageSize
               normalImageUrl:(NSString *)normalImageUrl
               selectImageUrl:(NSString *)selectImageUrl {
    self = [super init];
    if (self) {
        [self initDefaultParamWith:title normalTextColor:normalTextColor selectTextColor:selectTextColor normalImage:normalImage selectedImage:selectedImage];
        self.normalImageSize = normalImageSize;
        self.selectedImageSize = selectedImageSize;
        self.normalImageURL = normalImageUrl;
        self.selectedImageURL = selectImageUrl;
    }
    return self;
}


+(UIColor *)colorFromHexCode:(NSString *)hexString {
    
    NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@"0x" withString:@""];
    if ([cleanString length] == 3) {
        
        cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
                       [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                       [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                       [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
    }
    if([cleanString length] == 6) {
        cleanString = [cleanString stringByAppendingString:@"ff"];
    }
    
    unsigned int baseValue;
    [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
    float red = ((baseValue >> 24) & 0xFF)/255.0f;
    float green = ((baseValue >> 16) & 0xFF)/255.0f;
    float blue = ((baseValue >> 8) & 0xFF)/255.0f;
    float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}


#pragma mark - Private Method
- (void)initDefaultParamWith:(NSString *)title
             normalTextColor:(UIColor *)normalTextColor
             selectTextColor:(UIColor *)selectTextColor
                 normalImage:(UIImage *)normalImage
               selectedImage:(UIImage *)selectedImage {
    self.title = title;
    self.normalTextColor = normalTextColor;
    self.selectedTextColor = selectTextColor;
    self.normalImage = normalImage;
    self.selectedImage = selectedImage;
}


#pragma mark - Setter Method
- (void)setNormalImageSize:(CGSize)normalImageSize {
    _normalImageSize = [self imageSize:normalImageSize];
}


- (void)setSelectedImageSize:(CGSize)selectedImageSize {
    _selectedImageSize = [self imageSize:selectedImageSize];
}


- (CGSize)imageSize:(CGSize)imageSize {
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    width = (width >= MAX_TABBARITEM_WIDTH ? MAX_TABBARITEM_WIDTH: width);
    width = (width <= MIN_TABBARITEM_WIDTH ? MIN_TABBARITEM_WIDTH: width);
    height = (height >= MAX_TABBARITEM_HEIGHT ? MAX_TABBARITEM_HEIGHT: height);
    height = (height <= MIN_TABBARITEM_HEIGHT ? MIN_TABBARITEM_HEIGHT: height);
    return CGSizeMake(width, height);
}


@end
